let mta = require('./utils/mta_analysis.js');
App({
	onHide: function() {
		console.log('进入后台模式');
		var num = 1;
		var _self = this;
		this.tiemer = setInterval(function() {
			// console.log(num)
			num++;
			if (num == 1500) {
				getApp().globalData.common.shopCode = "";
			}
		}, 1000)

	},

	clearTime: function(fn) {
		clearInterval(this.tiemer)
	},
	onShow: function(options) {
		this.clearTime();
		if (wx.getUpdateManager) {
			const updateManager = wx.getUpdateManager()
			updateManager.onCheckForUpdate(function(res) {
				console.log('版本更新')
				console.log(res.hasUpdate)
			})
			updateManager.onUpdateReady(function() {
				wx.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success: function(res) {
						if (res.confirm) {
							updateManager.applyUpdate()
						}
					}
				})

			})
			updateManager.onUpdateFailed(function() {
				// 新的版本下载失败
			})
		}
		var _self = this;
		//  console.log("+++++++++");
	    //  console.log("不能获取getapp---"+getApp()); 
		//  console.log("获取openID---"+wx.getStorageSync('openId'));
		if (!getApp()) {
			return;
		}
		    this.getFaceLocation();		
			wx.request({
				url: getApp().globalData.baseUrl + '/applet/login.do',
				method: 'get',
				data: {
					openId: wx.getStorageSync('openId'),
				},
				success: function(res) {
					if (res.data.code == 200) {
						console.log("后台转前台999")
						getApp().globalData.userInfo.memId = res.data.obj.memId;
						getApp().globalData.userInfo.nickName = res.data.obj.nickName;
						getApp().globalData.userInfo.avatarUrl = res.data.obj.avatarUrl;
						getApp().globalData.userInfo.gender = res.data.obj.gender;
						getApp().globalData.userInfo.level = res.data.obj.level;
						getApp().globalData.userInfo.isBind = res.data.obj.isBind;
						getApp().globalData.header.Cookie = 'SESSION=' + res.data.obj.session;
						console.log(getApp().globalData.userInfo.memId)
					} else {
						console.log("出错");
						console.log(res.data.des);
					}
				}
			})
			
	},
	getFaceLocation: function() {
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/shop/getFaceLocation.do',
			method: 'get',
			header: getApp().globalData.header,
			data: {
				openId: wx.getStorageSync('openId'),
			},
			success: function(res) {
				console.log("人脸识别结果")
				//console.log(res)
				wx.hideLoading();
				if (res.data.code == 200) {
					//_self.showMyToast(_self, res.data.obj.shopCode, 10000)
					mta.Event.stat("smg_renlainshibie", {});
					wx.setStorageSync('shopCode', res.data.obj.shopCode);
					getApp().globalData.common.shopCode = res.data.obj.shopCode;
				}
			}
		})
	},
	onLaunch: function() {
		mta.App.init({
			"appID": "500572185",
			"eventID": "500577079", // 高级功能-自定义事件统计ID，配置开通后在初始化处填写
			// "lauchOpts":options, //渠道分析,需在onLaunch方法传入options,如onLaunch:function(options){...}
			// "statPullDownFresh":true, // 使用分析-下拉刷新次数/人数，必须先开通自定义事件，并配置了合法的eventID
			// "statShareApp":true, // 使用分析-分享次数/人数，必须先开通自定义事件，并配置了合法的eventID
			// "statReachBottom":true // 使用分析-页面触底次数/人数，必须先开通自定义事件，并配置了合法的eventID
		});
		let that = this;
		wx.getSystemInfo({ //  获取页面的有关信息
			success: function(res) {
				wx.setStorageSync('systemInfo', res)
				var ww = res.windowWidth;
				var hh = res.windowHeight;
				that.globalData.ww = ww;
				that.globalData.hh = hh;
			}
		});
	},
	globalData: {
		socketUrl: "wss://go.24xiaomai.com/websocket.do",//socket链接
		//socketUrl: "wss://tgo.24xiaomai.com/websocket.do", //
		otherUrl: 'https://investigation.24xiaomai.com', //问券调查
		baseUrl: 'https://scandc.24xiaomai.com',
		//baseUrl: 'https://tscandc.24xiaomai.com',
		imageUrl: 'http://static.24xiaomai.com/go/wxapplet/1.3.0/image',
		header: {
			'Cookie': '', //公用请求头
			'content-type': 'application/json'
		},
		common: {
			id: '',
			isFirst: true,
			shopCode: '', //门店信息
			shopName: '', //门店名字
			ST: '', //门店类型 1 现售  2 外卖
			souce: '', //来源 1 不开门 0 开门
			selfPay: null, //付款页跳转至『我的』页面时，订单发生了变化，要重新获取订单信息，wx.switchTab: url 不支持 queryString，使用见pay.js
			voucherMemberId: "", //优惠券id
			isAuto: "", //是否自动选择优惠券 Integer是否自动选择优惠券(有值时不自动选择)
			isBalance: false,
			mxShopCode: null, //所选麦享门店
			reserveShopCode: null, //所选商铺门店
			reserveShopName:null, //选择的字体地址
			reserveSelfAdd: null, //商品的自提地址
			reservebAdd: null, //商品选择配送地址
			reservesAdd: null, //商品选择配送详情地址
			addressId: "", //外卖地址ID
			reservesNote: null, //商品选择配送备注
			isScanPage: true, //扫码页面是否刷新购物车
			reserveVid: null, //商铺优惠券id
			longitude: '', //经度
			latitude: '', //维度
			recommendId: '', //推荐人id 
			groupCode: null, //拼团团号
			memberPage: 1,
			goodsCode: null,
			categoryCode: null,
			deviceCode: null,
			shopType:null,//店铺类型，1:校园店 2：盒子店 10：荣盛店
			passwordAddress:null,
			isOne:true,
			categoryCode:null, //当前选中的门店分类
			distributionStartTime:null,//门店开始时间
			distributionEndTime:null//门店结束时间
		},
		userInfo: {
			memId: "", //用户id
			openId: "", //唯一标示
			nickName: "", //名字
			avatarUrl: "", //头像 
			gender: "", //性别
			level: "", //会员级别
			isBind: false//是否绑定了手机号
		}
	},
	/*
	
	  parameter:
				url请求地址，
				data请求参数，
				method请求方法，
				header请求头
	 调用方法:getApp().getApi({参数}).then((res)=>{成功}).catch((err)=>{失败});			
	*/
	getRequest(parameter) {
		let that = this;
		var promise = new Promise((resolve, reject) => {
			wx.request({
				url: parameter.url,
				data: parameter.data ? parameter.data : "",
				method: parameter.method ? parameter.method : "get",
				header: parameter.header ? parameter.header : that.globalData.header,
				success: function(res) {
					if (res.data.code == 200) {
						resolve(res);
					} else {
						reject(res.data);
					}
				},
				fail: function(e) {
					reject(e);
				}
			});
		});
		return promise;
	},
	//请求小图片，检测是否有网络
	//this.urlTobase64('http://static.24xiaomai.com/go/wxapplet/1.3.0/image/mx_mine_next.png');
	urlTobase64(url) {
		var _self = this;
		wx.request({
			url: url,
			responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
			success: res => {
				let arraybuffer = res.data;
				let unit8Arr = new Uint8Array(arraybuffer);
				const base64 = wx.arrayBufferToBase64(unit8Arr)
				console.log('data:image/jpeg;base64,' + base64);
				_self.setData({
					urlImage: 'data:image/jpeg;base64,' + base64
				})
			}
		})
	}
});
/*
{
			"pagePath": "pages/newIndex/newIndex",
			"iconPath": "image/tab_icon_home_s.png",
			"selectedIconPath": "image/tab_icon_home_n.png",
			"text": "首页"
		}, {
			"pagePath": "pages/group/group",
			"iconPath": "image/tab_icon_pintuan_s.png",
			"selectedIconPath": "image/tab_icon_pintuan_n.png",
			"text": "拼团"
		}, 
*/